package com.emm.yixun.website.controller.marketing;
//框架包
import java.util.*;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;




//common
import com.emm.yixun.common.page.PageUtil;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.model.User;
//website
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;
//变化的引用
import com.emm.yixun.common.model.RoomChangeHistory;
import com.emm.yixun.common.model.dto.RoomChangeHistoryDto;
import com.emm.yixun.common.model.enums.RoomStatusEnum;
import com.emm.yixun.marketing.service.RoomChangeHistoryServiceFacade;

@Controller
@RequestMapping("/marketing/roomChangeHistory/")
public class RoomChangeHistoryController extends BaseController<RoomChangeHistory>{
	
	@Autowired
	private RoomChangeHistoryServiceFacade roomChangeHistoryServiceFacade;
	
	@RequestMapping(value="index")
	public String index(ModelMap map){
		if(doSecurityIntercept(Const.RESOURCES_TYPE_MENU)){
			//查询菜单按钮权限
			map.put("permitBtn", getPermitBtn(Const.RESOURCES_TYPE_FUNCTION));		
			return "/marketing/roomChangeHistory/list";
		}
		return Const.NO_AUTHORIZED_URL;
	}
	
	//新增返回实体对象
	@RequestMapping(value="add", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  add(RoomChangeHistoryDto dto){
    	AjaxRes ar=getAjaxRes();
    	
    	/*
    	 *1.判断权限 
    	 *2.调用新增接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"marketing:roomChangeHistory:add"))){
        	try {
        		User user=ShiroUtil.getCurrentUser();
        		dto.setMerchantId(user.getMerchantId());
        		dto.setCreateUserId(user.getId());
        		dto.setDelFlag("0");
        		dto.setUpdateUserId(user.getId());
        		dto.setUpdateTime(new Date());
				Response<RoomChangeHistoryDto> response=roomChangeHistoryServiceFacade.save(dto);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.SAVE_SUCCEED);
				}else{
					ar.setFailMsg(Const.SAVE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.SAVE_FAIL);
			}
    	}
    	return ar;
    }
	//查看
	@RequestMapping(value="view", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  view(Long id){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用查看接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"marketing:roomChangeHistory:view"))){
        	try {
				Response<RoomChangeHistory> response=roomChangeHistoryServiceFacade.findById(id);
				if(response.isSuccess()){
					ar.setSucceed(response.getEntity());
				}else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
    	}
    	return ar;
    }
	
	//修改
	@RequestMapping(value="edit", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  edit(RoomChangeHistoryDto dto){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用修改接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"marketing:roomChangeHistory:edit"))){
        	try {
        		Response<Integer> response=roomChangeHistoryServiceFacade.updateByPrimaryKey(dto, true);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.UPDATE_SUCCEED);
				}else{
					ar.setFailMsg(Const.UPDATE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.UPDATE_FAIL);
			}
    	}
    	return ar;
    }
	
	//删除
	@RequestMapping(value="delete", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  delete(Long id){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用查看接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"marketing:roomChangeHistory:delete"))){
        	try {
        		Response<Integer> response=roomChangeHistoryServiceFacade.deleteByPrimaryKey(id);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.DEL_SUCCEED);
				}else {
					ar.setFailMsg(Const.DEL_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DEL_FAIL);
			}
    	}
    	return ar;
    }
	
	//分页查询
	@RequestMapping(value="list")
	@ResponseBody
    public Page<RoomChangeHistory> list(RoomChangeHistoryDto dto,Integer page,Integer limit){
        	try {
        		User user = ShiroUtil.getCurrentUser();
        		dto.setMerchantId(user.getMerchantId());
        		dto.setPermissionUserId(user.getId());
        		dto.setResourcesCode("menu_0005_1000");
        		Page<RoomChangeHistory> response=roomChangeHistoryServiceFacade.findPageByDto(dto, PageUtil.begin(page, limit),limit);
                if(response.getSuccess()){
                    if(null==response.getRows()){
                    	response.setRows(new ArrayList<RoomChangeHistory>());
                    }else{
                    	response.setRows(roomChangeHistoryLinkInfo(response.getRows()));
                    }
                	response.setPageNumber(limit);
                	return response;
                }
			} catch (Exception e) {
				logger.error(e.toString(),e);
			}
    	return null;
    }
	
	/**
	* 方法描述 : 变更记录详情按钮查看权限
	* 创建者：ThinkPad 
	* 项目名称： emm-website
	* 类名： RoomChangeHistoryController.java
	* 版本： v1.0
	* 创建时间： 2016年7月22日 上午10:16:14
	* @param list
	* @return List<RoomChangeHistory>
	 */
	@SuppressWarnings("unchecked")
	public List<RoomChangeHistory> roomChangeHistoryLinkInfo(List<RoomChangeHistory> list){
		String btnArray = getPermissionBtnArray();
		JSONArray array = JSONArray.fromObject(btnArray);
		List<String>  btnArrayList = (List<String>)JSONArray.toCollection(array);
		for (RoomChangeHistory history:list) {
			if(history.getBusinessDynamics().equals(RoomStatusEnum.RETAIN.getDes())){
				if(!btnArrayList.contains("marketing:retain:view")){
					history.setBusinessHref("");
				}
			}else if(history.getBusinessDynamics().equals(RoomStatusEnum.MA.getDes())){
				if(!btnArrayList.contains("marketing:sellControl:view")){
					history.setBusinessHref("");
				}
			}else if(history.getBusinessDynamics().equals(RoomStatusEnum.RESERVE.getDes())){
				if(!btnArrayList.contains("marketing:payDeposit:view")){
					history.setBusinessHref("");
				}
			}else if(history.getBusinessDynamics().equals(RoomStatusEnum.BUY.getDes())){
				if(!btnArrayList.contains("marketing:subscribe:view")){
					history.setBusinessHref("");
				}
			}else if(history.getBusinessDynamics().equals(RoomStatusEnum.SIGN.getDes())){
				if(!btnArrayList.contains("marketing:signed:view")){
					history.setBusinessHref("");
				}
			}else if(history.getBusinessDynamics().equals("换房")){
				if(!btnArrayList.contains("marketing:subscribe:changeHouseView")){
					history.setBusinessHref("");
				}
			}else if(history.getBusinessDynamics().equals("退认筹")){
				if(!btnArrayList.contains("marketing:payDeposit:retreatView")){
					history.setBusinessHref("");
				}
			}
		}
		return list;
	}
	
}
